package com.jtoon.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jtoon.bean.Comics;
import com.jtoon.bean.Episode;
import com.jtoon.bean.Img;
import com.jtoon.util.PMF;

public class EpisodeManage extends HttpServlet {

	public static final int ADD = 1;
	public static final int UPDATE = 2;
	public static final int DELETE = 3;
	public static final int LIST = 4;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		long cid = 0;
		int mode = 4;
		
		if(request.getParameter("cid") != null){
			cid = Long.valueOf(request.getParameter("cid"));
		}
		
		if(request.getParameter("mode") != null){
			mode = Integer.valueOf(request.getParameter("mode"));
		}
		
		switch (mode) {
			case ADD :	episodeAdd(request, response, cid); break;
			case UPDATE : episodeUpdate(request, response, cid); break; 
			case DELETE : episodeDelete(request, response, cid); break;
			case LIST : episodeList(request, response, cid); break;
		}
		
	}
	
	public void episodeAdd(HttpServletRequest request, HttpServletResponse response, long cid) throws ServletException, IOException {
		 
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String comicsTitle = "";
		String title = request.getParameter("title");
		Query query = pm.newQuery(Episode.class);
		
		try{
			int order = Integer.valueOf(request.getParameter("order"));
			Comics comics = pm.getObjectById(Comics.class, Long.valueOf(cid));
			comicsTitle = comics.getTitle();
			Episode episode = new Episode(title, order, cid);		
			
			pm.makePersistent(episode); 
			
			query.setFilter("cid == cidParam");
			query.declareParameters("long cidParam");
			query.setOrdering("order desc");
			List<Episode> episodeList = (List<Episode>)query.execute(cid);
			request.setAttribute("episodeList", episodeList);
			
			RequestDispatcher rd = request.getRequestDispatcher("/manage/episodeManage.jsp"); 
			request.setAttribute("comicsTitle", comicsTitle);
			request.setAttribute("cid", cid);
			rd.forward(request, response);   
		}finally{
			query.closeAll();
			pm.close(); 
		} 
	} 
	
	public void episodeUpdate(HttpServletRequest request, HttpServletResponse response, long cid) throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String comicsTitle = "";
		
		try{
			Comics comics = pm.getObjectById(Comics.class, Long.valueOf(cid));
			comicsTitle = comics.getTitle();
			
		}finally{
			 
		}
		
		RequestDispatcher rd = request.getRequestDispatcher("/manage/episodeManage.jsp"); 
		request.setAttribute("cid", cid);
		request.setAttribute("comicsTitle", comicsTitle);
		rd.forward(request, response);   	
	}
	
	public void episodeDelete(HttpServletRequest request, HttpServletResponse response, long cid) throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String comicsTitle = "";
		Query query = pm.newQuery(Episode.class);
		try{
			Comics comics = pm.getObjectById(Comics.class, Long.valueOf(cid));
			comicsTitle = comics.getTitle();
			
			long eid = Long.valueOf(request.getParameter("eid"));
			Episode episode = pm.getObjectById(Episode.class, eid);
			
			pm.deletePersistent(episode);
			  
			query.setFilter("cid == cidParam");
			query.declareParameters("long cidParam");
			query.setOrdering("order desc");
			List<Episode> episodeList = (List<Episode>)query.execute(cid);
			request.setAttribute("episodeList", episodeList);
			
			RequestDispatcher rd = request.getRequestDispatcher("/manage/episodeManage.jsp"); 
			request.setAttribute("cid", cid);
			request.setAttribute("comicsTitle", comicsTitle);
			rd.forward(request, response);    
		}finally{ 
			query.closeAll();
			pm.close(); 
		}
		
		
	}
	
	public void episodeList(HttpServletRequest request, HttpServletResponse response, long cid) throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String comicsTitle = "";
		Query query = pm.newQuery(Episode.class);
		try{
			Comics comics = pm.getObjectById(Comics.class, Long.valueOf(cid));
			comicsTitle = comics.getTitle();
			
			query.setFilter("cid == cidParam");
			query.declareParameters("long cidParam");
			query.setOrdering("order desc");
			List<Episode> episodeList = (List<Episode>)query.execute(cid);
			request.setAttribute("episodeList", episodeList); 
			
			RequestDispatcher rd = request.getRequestDispatcher("/manage/episodeManage.jsp"); 
			request.setAttribute("comicsTitle", comicsTitle);
			request.setAttribute("cid", cid);
			rd.forward(request, response);  
		}finally{
			query.closeAll();
			pm.close(); 
		} 
		 
	}
}
